package com.example.starseas_phone;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;

import com.example.starseas_phone.Adapters.ProfessionalManagelistviewAdapter;
import com.example.starseas_phone.Entity.College;
import com.example.starseas_phone.Entity.Professional;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class ProfessionalManageActivity extends AppCompatActivity {

    private ListView ProfessionalManage_list;
    private List<Professional> list = new ArrayList<>();
    private ProfessionalManagelistviewAdapter adapter;

    private FloatingActionButton ProfessionalManage_floatbtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_professional_manage);
        initid();
        initview();

        ProfessionalManage_floatbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(ProfessionalManageActivity.this,AddProfessionalActivity.class);
                startActivity(intent);
            }
        });
    }

    private void initid(){
        ProfessionalManage_floatbtn = (FloatingActionButton) findViewById(R.id.ProfessionalManage_floatbtn);
        ProfessionalManage_list = (ListView) findViewById(R.id.ProfessionalManage_list);
        adapter = new ProfessionalManagelistviewAdapter(ProfessionalManageActivity.this,R.layout.collegemanage_listview,list);
    }

    private void initview(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient client = new OkHttpClient();
                    Request request = new Request.Builder()
                            .url(Sto.NetIP+"/api/Professional/GetAllProfessionals")
                            .get()
                            .build();
                    Response response = client.newCall(request).execute();
                    String responseString = response.body().string();
                    JSONObject jsonObject = new JSONObject(responseString);
                    String msg = jsonObject.getString("message");
                    JSONArray jsonArray = new JSONArray(msg);
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jsonObjects = jsonArray.getJSONObject(i);
                        int ProfessionalId = jsonObjects.getInt("ProfessionalId");
                        String ProfessionalName = jsonObjects.getString("ProfessionalName");
                        int CollegeId = jsonObjects.getInt("CollegeId");
                        Professional professional = new Professional(ProfessionalId,ProfessionalName,CollegeId);
                        list.add(professional);
                    }
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            adapter.notifyDataSetChanged();
                            ProfessionalManage_list.setAdapter(adapter);
                        }
                    });
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }).start();
    }

    @Override
    protected void onRestart() {
        list.clear();
        initview();
        super.onRestart();
    }
}